from C45_Decision_Tree import DecisionTree
import numpy as np
import pandas as pd
import random as rd

# 读取训练数据集
dt = DecisionTree()
data = pd.read_excel('/Users/liuyuanxi/学习/华为智能基座/附件2.课程资源/数据集/客户信息及违约表现.xlsx')
data = np.array(data)
print('数据集：\n', data)
data_length = len(data)  # 数据集的长度
testData = []  # 创建测试集
testData_length = data_length * 0.7  # 训练集的长度
for i in range(int(testData_length)):
    testData.append(data[rd.randint(0, testData_length)])
print('测试集：\n', testData)
modelTree = dt.create_decision_tree(testData, ['收入', '年龄', '性别', '历史授信额度', '历史违约次数'])
# 打印决策树
print('决策树模型：\n', modelTree)
# 使用决策树模型进行预测
defaultUsersList = []  # 违约用户名单
observantUsersList = []  # 守约用户名单
for i in range(int(testData_length)):
    classLabel = dt.predic(modelTree, ['收入', '年龄', '性别', '历史授信额度', '历史违约次数'], testData[i])
    if classLabel == 1:
        defaultUsersList.append(i + 1)
    else:
        observantUsersList.append(i + 1)
print("守约用户名单：\n", observantUsersList, "\n违约用户名单：\n", defaultUsersList)
